iT邦幫忙

2022 iThome 鐵人賽

DAY 13
0
Software Development

戀戀 Elixir系列 第 13

十三. Case & Charlist

  • 分享至 

  • xImage
  •  

You are working as a system administrator for a big company in Munich, Germany. One of your responsibilities is managing email accounts.

You have been hearing complaints from people saying they are unable to write emails to Mr. Müller. You quickly realize that most of the company uses an old email client that doesn't recognize müller@firma.de as a valid email address because of the non-Latin character.

Telling people to give up their favorite old email client is a lost battle, so you decide to create sanitized aliases for all email accounts.

1. Sanitize existing usernames by removing everything but lowercase letters

Implement the sanitize/1 function. It should accept a username as a charlist and return the username with all characters but lowercase letters removed.

2. Allow underscores

Extend the sanitize/1 function. It should not remove underscores from the username.

3. Substitute German characters

There are 4 non-Latin characters in the German alphabet, and all of them have commonly-recognized Latin substitutes.

Extend the sanitize/1 function. It should substitute German characters according to the table. You can safely assume all usernames are already downcase.


https://exercism.org/tracks/elixir/exercises/german-sysadmin

defmodule Username do
  @moduledoc """
    practice case and charlist
  """

  @doc """
    a - z === 97 - 122
  """
  @spec sanitize(charlist) :: charlist
  def sanitize(''), do: ''
  def sanitize([head | tail]) do
    first_letter = case head do
      ?ä -> 'ae'
      ?ö -> 'oe'
      ?ü -> 'ue'
      ?ß -> 'ss'
      ?_ -> '_'
      head when head in ?a..?z -> [head]
      _ -> ''
    end

    first_letter ++ sanitize(tail)
  end
end

上一篇
十二. Typespecs
下一篇
十四. IO
系列文
戀戀 Elixir30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言